
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import languages.*; 

public class main {
	final static String FILENAME = "casebase"; 
	public static void main (String [] args) throws IOException {
		
		ArrayList<Case> cases = new ArrayList<Case>();
		
		// read dem files y0
        try{
		  FileInputStream fstream = new FileInputStream(FILENAME);

		  DataInputStream in = new DataInputStream(fstream);
		  BufferedReader br = new BufferedReader(new InputStreamReader(in));
		  
		  String strLine;
		  while ((strLine = br.readLine()) != null)   {
			  String[] result = strLine.split(";\\s");
			  
			  // skip comments
			  if(!(strLine.charAt(0) == '#')) {
				  if(result.length != 10) {
					  throw new Exception("Wrong number of parameters, please check the casebase-text file for errors");
				  }
				  
				  Language language = null;
				  int lang = Integer.parseInt(result[5]); 
				  
				  switch(lang){
					case 1:
						language = new C();
						break;
					case 2:
						language = new Java();
						break;
					case 3:
						language = new Python();
						break;
					case 4:
						language = new Ruby();
						break;
					case 5:
						language = new Scala();
						break;
				  }
				  
				  boolean webbased = false, crossplatform = false; 
				  
				  	if(result[1].compareTo("Y") == 0 || result[1].compareTo("y") == 0){
					  webbased = true;
					}
					
					if(result[2].compareTo("Y") == 0 || result[2].compareTo("y") == 0){
						crossplatform = true;
					}
					
				  Case newCase = new Case(
						  Integer.parseInt(result[0]), 
						  webbased, 
						  crossplatform, 
						  (long) Integer.parseInt(result[3]),
						  Integer.parseInt(result[4]), 
						  Integer.parseInt(result[5]), 
						  (long) Integer.parseInt(result[6]), 
						  Integer.parseInt(result[7]), 
						  Integer.parseInt(result[8]), 
						  language); 
				  cases.add(newCase); 
				  
			  }
			 
		  }
		  System.out.println("** Computar: Loaded " + cases.size() + " old cases **");
		  System.out.println("==================================\n");
		  in.close();
		}catch (Exception e) {
			System.err.println("Error: " + e.getMessage());
        }
		
		Input input = new Input();
		Case theCase = input.makeCase();
		
		

        Similarity sim = new Similarity();
        Case simCase = sim.mostSimilar(theCase, cases);
		
		Adaptation testAdoption = new Adaptation(theCase, simCase); 
		System.out.println("Result: " + testAdoption.fetchResult());
		
		//write the new case to file:
		theCase.effortDays = testAdoption.fetchResult().personDays;
				try {
			        BufferedWriter out = new BufferedWriter(new FileWriter(FILENAME, true));
			        out.append(theCase.toFileString());
			        
			        out.close();
			    } catch (IOException e) {
			    	System.out.println("Something went wrong and the new case has not been saved");
			    }
	
	}
}	

